package com.mingzhi.order.service;

import com.mingzhi.order.mapper.OrderMapper;
import com.mingzhi.order.model.OrderInfo;
import com.mingzhi.order.model.ProductInfo;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * DiscoveryClient 发现的客户端
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    //计数器
    //定义一个原子类 处理多线程
    private AtomicInteger count = new AtomicInteger(1);

    //从Eureka中获取服务信息列表
    private List<ServiceInstance> instances;

    /**
     * 初始化方法
     * 请求服务地址实例
     */
    //程序如果上线或者下限，不能立即感知，这里有bug
    @PostConstruct
    public void init() {
        instances = discoveryClient.getInstances("product-service");
    }

    /*public OrderInfo selectOrderById(Integer orderId) {
        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//        String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId();
        //从Eureka中获取服务信息列表
        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
        String uri = instances.get(0).getUri().toString();
        String url = uri + "/product/" + orderInfo.getProductId();
        //远程调用日志
        log.info("远程调用日志url:{}", url);
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }*/


    /*public OrderInfo selectOrderById(Integer orderId) {
        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//        String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId();

        //计算轮流实例访问的下标
        int index = count.getAndIncrement() % instances.size();
        //从Eureka中获取服务信息列表
        String uri = instances.get(index).getUri().toString();
        //拼接请求地址
        String url = uri + "/product/" + orderInfo.getProductId();
        //远程调用日志
        log.info("远程调用日志url:{}", url);
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }*/

    public OrderInfo selectOrderById(Integer orderId) {
        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
        String url = "http://product-service/product/" + orderInfo.getProductId();

        //远程调用日志
        log.info("远程调用日志url:{}", url);
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }
}
